import re

b = "组图共6张"
print(re.search("组图共(\d)张", b).group(1))

url = "https://weibo.cn/search/mblog?hideSearchFrame=&keyword=%23%E5%A5%BD%E7%89%A9%E5%88%86%E4%BA%AB%23&advancedfilter=1&hasori=1&starttime=20190120&endtime=20200119&sort=time&page=2"
print(re.search("page=(\d*)",url).group(1))
print(re.sub("page=\d*", "page="+str(18), url))

pattern = "(page=)(\d*)"

def func(match):  # func的参数是一个match对象
    page = match.group(2)
    page = int(page) + 1
    return '%s%s' % (match.group(1), str(page))

print(re.sub(pattern, func, url))

text = 'Jame 12 years old'
pattern = r'([A-Z]\w+)\s(\d{1,2})'
result = re.sub(pattern, r'\1 is \2', text)
print(result)

s = "2017-01-22"

print (re.sub("(\d{4})-(\d{2})-(\d{2})",r"\2/\3/\1",s))
print (re.sub("(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})","\g<month>/\g<day>/\g<year>",s))
# 01/22/2017
# 01/22/2017


astr = 'aabb aacc aadd'
result = re.sub(r'(?i)aa(?P<pattern>[\w]+)',r'bb\g<pattern>',astr)
print('{} has been substituted to {}'.format(astr,result))


